package com.aigou.admin.aigouspringboot.controller.user;

import com.aigou.admin.aigouspringboot.common.LogScope;
import com.aigou.admin.aigouspringboot.entity.ReceivingAddress;
import com.aigou.admin.aigouspringboot.result.Result;
import com.aigou.admin.aigouspringboot.service.ReceivingAddressService;
import com.aigou.admin.aigouspringboot.utils.RandomUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController("userReceivingAddressController")
    @RequestMapping("/user/receivingaddress")
public class ReceivingAddressController {
    @Resource
    private ReceivingAddressService receivingAddressService;

    @GetMapping("/user/{userId}")
    @LogScope(title = "收货地址管理模块", content = "根据user_id查询收货地址信息")
    public Result selectByUserId(@PathVariable String userId) {
        List<ReceivingAddress> receivingAddresses = receivingAddressService.selectByUserId(userId);
        return Result.success(receivingAddresses);
    }

    @PostMapping("/usersave")
    @LogScope(title = "收货地址管理模块", content = "添加用户收货地址信息")
    public Result save(@RequestBody ReceivingAddress receivingaddress){

        receivingaddress.setId(RandomUtil.getRandomNumber());
        int i = receivingAddressService.insertAddress(receivingaddress);
        return i>0 ? Result.success() : Result.error();
    }
    @PutMapping("/isDefault")
    @LogScope(title = "收货地址管理模块", content = "编辑默认收货地址信息")
    public Result editisDefault(@RequestBody ReceivingAddress receivingaddress){

        int i = receivingAddressService.updateisDefaultAddresses(receivingaddress);
        return i>0 ? Result.success() : Result.error();
    }
    @GetMapping("/{id}")
    @LogScope(title = "收货地址管理模块", content = "根据id查询收货地址信息")
    public Result detail(@PathVariable String id){
        ReceivingAddress receivingaddress = receivingAddressService.selectById(id);
        return Result.success(receivingaddress);
    }

    @PutMapping("/edit")
    @LogScope(title = "收货地址管理模块", content = "编辑收货地址信息")
    public Result edit(@RequestBody ReceivingAddress receivingaddress){

        int i = receivingAddressService.edit(receivingaddress);
        return i>0 ? Result.success() : Result.error();
    }
    @DeleteMapping("/{id}")
    @LogScope(title = "收货地址管理模块", content = "根据id删除收货地址信息")
    public Result delete(@PathVariable String id ,@RequestHeader("Authorization") String token){
        int i = receivingAddressService.deleteById(id,token);
        return i>0 ? Result.success() : Result.error();
    }
}
